package graph.day06;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Kama_105 {

    static List<List<Integer>> list;

    static boolean[] visited;

    private static void dfs(int start) {
        if (visited[start]) {
            return;
        }
        visited[start] = true;
        List<Integer> canReach = list.get(start);
        for (int i = 0; i < canReach.size(); i++) {
            Integer end = canReach.get(i);
            dfs(end);
        }
    }


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        list = new ArrayList<>(n + 1);
        for (int i = 0; i < n + 1; i++) {
            list.add(new ArrayList<>());
        }
        visited = new boolean[n + 1];
        for (int i = 0; i < k; i++) {
            int start = sc.nextInt();
            int end = sc.nextInt();
            list.get(start).add(end);
        }
        dfs(1);
        int res = 1;
        for (int i = 1; i < visited.length; i++) {
            if (!visited[i]) {
                res = -1;
                break;
            }
        }
        System.out.println(res);
    }
}
